package com.xinkao.example.controller;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.xinkao.example.config.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.xinkao.example.util.CommonUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import com.xinkao.example.domain.entity.LogEntity;
import com.xinkao.example.service.LogService;


/**
 * 后台操作日志表
 * @Author
 * @Date 2021-08-25 09:24:46
 */
@RestController
@Api(tags = "后台操作日志表")
@RequestMapping("/log")
public class LogController extends BaseController{
    @Resource
    private LogService logService;

    /**
     * 分页查询
     */
    @RequiresPermissions("log:list")
    @ApiOperation(value = "分页查询", response = LogEntity.class)
    @PostMapping("/list")
    public JSONObject list(@RequestBody JSONObject requestJson){
        CommonUtil.hasAllRequired(requestJson, "pageNum, pageRow");
        return logService.list4Page(requestJson);
    }


    /**
     * 列表查询
     */
    @ApiOperation(value = "列表查询", response = LogEntity.class)
    @PostMapping("/listByCondition")
    public JSONObject listByCondition(@RequestBody JSONObject requestJson){
        return logService.listByCondition(requestJson);
    }


    /**
     * 查询详情
     */
    @RequiresPermissions("log:list")
    @ApiOperation(value = "查询详情", response = LogEntity.class)
    @PostMapping("/info/{id}")
    public JSONObject info(@PathVariable("id") String id){
        return logService.queryOneById(id);
    }


    /**
     * 保存
     */
    @RequiresPermissions(value={"log:save","log:edit"},logical= Logical.OR)
    @ApiOperation(value = "保存", response = JSONObject.class)
    @PostMapping("/save")
    public JSONObject save(@RequestBody LogEntity entity){
        if(StrUtil.isEmpty(entity.getId())){
            entity.setCreateBy(getUserId());
        }else{
            entity.setUpdateBy(getUserId());
        }
        return logService.insertOrEdit(entity);
    }

    /**
     * 删除/批量删除
     */
    @RequiresPermissions("log:delete")
    @ApiOperation(value = "删除/批量删除", response = JSONObject.class)
    @PostMapping("/delete")
    public JSONObject delete(@RequestBody List<String> ids){
        return logService.deleteLogic(ids, getUserId());
    }

}
